package com.libbase;

import android.content.Context;
import android.support.multidex.MultiDex;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.BaseApplication;
import com.base.network.Neter;
import com.libbase.bugly.BuglyHelper;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.tencent.bugly.beta.Beta;

public class LibApplication extends BaseApplication {

    private static LibApplication instance;
    private RefWatcher refWatcher;

    public static LibApplication getInstance() {
        return instance;
    }

    @Override
    protected void init() {
        instance = this;
        ARouter.openLog();     // 打印日志
        ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        ARouter.init(this);
        Neter.Companion.init(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
        // 安装tinker
        Beta.installTinker();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        BuglyHelper.getInstance().init(getApplicationContext()); // bugly多渠道包
    }

    @Override
    protected void initAllProcess() {
        if (!LeakCanary.isInAnalyzerProcess(this)) {
            refWatcher = setupLeakCanary();
        }
    }

    private RefWatcher setupLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return RefWatcher.DISABLED;
        }
        return LeakCanary.install(this);
    }

    public RefWatcher getRefWatcher() {
        return refWatcher;
    }

    @Override
    public void onTerminate() {
        ARouter.getInstance().destroy();
        //Beta.unInit();
        super.onTerminate();
    }
}
